﻿using System.IO;
using Catel.IoC;
using Catel.Runtime.Serialization.Xml;

namespace YtbDownloader.Services;

public class ConfigService(string configPath) : IConfigService
{
    public T Load<T>() where T : new()
    {
        if (!File.Exists(configPath)) return new T();
        using var input = File.OpenRead(configPath);
        return (T)ServiceLocator.Default.ResolveType<IXmlSerializer>().Deserialize(typeof(T), input);
    }

    public void Save<T>(T config)
    {
        using var output = File.OpenWrite(configPath);
        ServiceLocator.Default.ResolveType<IXmlSerializer>().Serialize(config, output);
    }
}